package com.cloud.pole.shop.controller.shops.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.common.util.TreeUtil;
import com.cloud.pole.shop.biz.goods.ShopGoodsBiz;
import com.cloud.pole.shop.biz.shops.ShopShopsBiz;
import com.cloud.pole.shop.controller.goods.admin.GoodsAdminController;
import com.cloud.pole.shop.model.entity.goods.ShopGoods;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsAdmin;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsCategory;
import com.cloud.pole.shop.model.entity.shops.ShopShops;
import com.cloud.pole.shop.model.entity.shops.ShopShopsAdmin;
import com.cloud.pole.shop.model.entity.shops.ShopShopsWithBLOBs;
import com.cloud.pole.shop.model.vo.goods.GoodsCategoryTree;
import com.cloud.pole.shop.model.vo.shops.ShopShopsTree;
import com.cloud.pole.shop.service.shops.ShopShopsService;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/*
*2019年7月1日
*cloud-shop
*author:frank-chen
*/
@RestController
@RequestMapping("/shops/admin_merch")
@Slf4j
public class ShopsAdminMerchantController extends BaseController<ShopShopsBiz,ShopShops>{
	@Autowired
    private ShopShopsService shopShopsService;
	
	@RequestMapping(value = "/getAllShopList", method = RequestMethod.GET)
	@ResponseBody
	public ApiRestResponse<List<ShopShopsWithBLOBs>> getAllShopList(
			@RequestHeader(defaultValue = "") String token) {
		try {
			//获取商家信息
			List<ShopShopsWithBLOBs> shopList = shopShopsService.getAllShopList(token);
		    return ApiRestResponse.success("成功",shopList);
		}
		catch (BaseException e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} 
		catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}
	
    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    @ResponseBody
    public List<ShopShopsTree> getTree(
    		@RequestHeader(defaultValue = "") String token,
    		String title) {
        List<ShopShopsWithBLOBs> shopList =  null;
		try {
			shopList = shopShopsService.getAllShopList(token);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
		}
        return getShopsTree(shopList, 0);
    }

    private List<ShopShopsTree> getShopsTree(List<ShopShopsWithBLOBs> list,int root) {
        List<ShopShopsTree> trees = new ArrayList<ShopShopsTree>();
        ShopShopsTree node = null;
        for (ShopShops shop : list) {
            node = new ShopShopsTree();
            node.setId(shop.getShopId());
            node.setParentId(0);
            node.setName(shop.getShopName());
            node.setMerchantId(shop.getMerchantId());
            trees.add(node);
        }
        return TreeUtil.bulid(trees,root) ;
    }
}
